<?php
namespace App\Controller;



use App\Model\ItemsModel;
use App\Model\TopicModel;
use Common\Util\ArrayToCsvUtils;
use Common\Util\CacheUtils;

class TestController extends BaseController
{



    //检查文件图片不存在
    public function checkFiles(){

        $page_no =$this->getPostParam('page_no',1);
        $page_size =$this->getPostParam('page_size',5000);
        $city_code=['C013SHENZHEN','C016GUANGZHOU'];
        $list = ItemsModel::singleton()->getItems($city_code,$page_no,$page_size);
        $data =[];
        foreach ($list as $key => $val){
            $file='./Public/item_img/K'.$val['item_code'].".jpg";
            $file1='./Public/item_img/'.$val['item_key'].".jpg";
            if(file_exists($file) || file_exists($file1)){
                $data[]=[
                    'item_key'=>$val['item_key'],
                    'item_name'=>$val['item_name'],
                    'topic_field'=>$val['topic_field'],
                    'item_code'=>$val['item_code']
                ];
            }
/*830+252=1087*/
        }

        $csv = new ArrayToCsvUtils();

        $filename =  date('YmdHis') . '.csv';
        $csv->cvsHeader($filename);
        $head= ['item_key',
            'item_name',
            '主题字段',
            'item_code'
            ];

        $csv->outputData($head);

        foreach($data as $key=>$val){
            $item =[
                $val['item_key'],
                $val['item_name'],
                $val['topic_field'],
                $val['item_code']
            ];
            $csv->outputData($item);
        }
        $csv->closeFile();
    }



    //导出图片宽高
    public function importHW(){
        $data =$this->read_all('./Public/item_img');

        foreach ($data as  $key=>$val){

            $file_name = basename($val,".jpg");
            $file_info = getimagesize($val);
            $width = $file_info[0];
            $hight = $file_info[1];
            if(ItemsModel::singleton()->where(['item_key'=>$file_name])->find()){
                ItemsModel::singleton()->where(['item_key'=>$file_name])->save(['height'=>$hight,'width'=>$width,'img_name'=>$file_name]);
            }
            if(ItemsModel::singleton()->where(['item_code'=>substr($file_name,1)])->find()){
                ItemsModel::singleton()->where(['item_code'=>substr($file_name,1)])->save(['height'=>$hight,'width'=>$width,'img_name'=>$file_name]);
            }


        }

    }

    private function read_all ($dir){
        $data =[];
        if(!is_dir($dir)) return false;
        $handle = opendir($dir);
        if($handle){
            while(($fl = readdir($handle)) !== false){
            $temp = $dir.DIRECTORY_SEPARATOR.$fl;
            //如果不加  $fl!='.' && $fl != '..'  则会造成把$dir的父级目录也读取出来
            if(is_dir($temp) && $fl!='.' && $fl != '..'){
                      echo '目录：'.$temp.'<br>';
                 read_all($temp);
            }else{
                               if($fl!='.' && $fl != '..'){

                     //echo '文件：'.$temp.'<br>';
                     $data[]=$temp;
                }
            }
        }
        return $data;
     }
    }
}